package com.chonglie;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

public class ActivityLifecycleActivity1 extends Activity {
	public static final String MSG = "msg";
	public static final String DEFAULT_URL = "http://www.google.com.hk";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_lifecycle1);
		Log.e("Activity1", "onCreate");

	}

	@Override
	protected void onStart() {
		super.onStart();
		Log.e("Activity1", "onStart");
	}

	@Override
	protected void onResume() {
		super.onResume();
		Log.e("Activity1", "onResume");
		SharedPreferences preferences = getPreferences(0);

		EditText editText = (EditText) findViewById(R.id.activit_lifecycle1_text);
		editText.setText(preferences.getString(ActivityLifecycleActivity1.MSG, ""));
	}

	@Override
	protected void onPause() {
		super.onPause();
		Log.e("Activity1", "onPause");
		EditText editText = (EditText) findViewById(R.id.activit_lifecycle1_text);
		SharedPreferences.Editor editor = getPreferences(0).edit();
		editor.putString(ActivityLifecycleActivity1.MSG, editText.getText().toString());
		editor.commit();
	}

	@Override
	protected void onStop() {
		super.onStop();
		Log.e("Activity1", "onStop");
	}

	@Override
	protected void onRestart() {
		super.onRestart();
		Log.e("Activity1", "onRestart");
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		Log.e("Activity1", "onDestroy");
	}

	public void toActiviy2(View view) {
		EditText editText = (EditText) findViewById(R.id.activit_lifecycle1_text);
		Intent intent = new Intent(this, ActivityLifecycleActivity2.class);
		intent.putExtra(MSG, editText.getText().toString());
		startActivity(intent);
	}

	public void toExternalActivity(View view) {
		Log.e("Activity1", "toExternalActivity");
		EditText editText = (EditText) findViewById(R.id.activit_lifecycle1_text);
		Uri uri = Uri.parse(editText.getText().toString());
		Intent intent = new Intent(Intent.ACTION_VIEW, uri);
		try {
			startActivity(intent);
		} catch (Exception e) {
			Log.e("Activity1", e.getMessage());
		}

	}
}
